package com.itheima.ydd.controller;


import com.itheima.ydd.common.R;
import com.itheima.ydd.dto.AdvposDto;
import com.itheima.ydd.dto.PageInfo;
import com.itheima.ydd.pojo.YddAdvpos;
import com.itheima.ydd.service.YddAdvposService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("ydd_advpos")
@Slf4j
public class YddAdvposController {
    @Autowired
    private YddAdvposService yddAdvposService;

    /**
     * 广告位修改
     * @param yddAdvpos
     * @return
     */
    @PostMapping("update")
    public ResponseEntity<R<String>> update(@RequestBody YddAdvpos yddAdvpos){
        return yddAdvposService.updateYddAdvops(yddAdvpos);
    }

    /**
     * 广告位删除
     * @param id
     * @return
     */
    @GetMapping("del")
    public ResponseEntity<R<String>> del(@RequestParam Integer id){
        return yddAdvposService.delYddAdvops(id);
    }

    /**
     * 广告位添加
     * @param yddAdvpos
     * @return
     */
    @PostMapping("add")
    public ResponseEntity<R<String>> add(@RequestBody YddAdvpos yddAdvpos){
        return yddAdvposService.addYddAdvops(yddAdvpos);
    }


    /**
     * 分页
     * @param page 页码
     * @param limit 每页数量
     * @param advposdesc    广告位名称
     * @param advposname    广告位描述
     * @return
     */
    @GetMapping("list")
    public ResponseEntity<R<PageInfo<AdvposDto>>> list (@RequestParam (value = "page",defaultValue = "1") Integer page ,
                                                        @RequestParam (value = "limit" ,defaultValue = "10") Integer limit,
                                                        @RequestParam (value = "advposdesc") String advposdesc,
                                                        @RequestParam (value = "advposname") String advposname){
        return yddAdvposService.findPage(page,limit,advposdesc,advposname);

    }

    /**
     * 根据id查询数据
     * @param id
     * @return
     */
    @GetMapping("rowInfo")
    public ResponseEntity<R<YddAdvpos>> findId(@RequestParam(value = "id") String id){
        YddAdvpos yddAdvpos = yddAdvposService.getById(id);
        log.info(yddAdvpos.toString());
        return ResponseEntity.ok(R.success(yddAdvpos));
    }

    /**
     *查询全部数据
     */
    @GetMapping("all")
    public ResponseEntity<R<List<YddAdvpos>>> findAll(){
        return yddAdvposService.findAllData();
    }
}
